package cc.comac.mom.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipRepairRepositoryRepairStepService;
import cc.comac.mom.bean.EquipRepairRepository;
import cc.comac.mom.bean.EquipRepairRepositoryRepairStep;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.datahandlers.entity.EquipRepairRepositoryDataHandler;
import cc.comac.mom.datahandlers.entity.EquipRepairRepositoryRepairStepDataHandler;
import cc.comac.mom.entity.EquipRepairRepositoryEntity;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
public class EquipRepairRepositoryRepairStepServiceImpl implements EquipRepairRepositoryRepairStepService {
	@Autowired
	private EquipRepairRepositoryRepairStepDataHandler EquipRepairRepositoryRepairStepDataHandler;
	@Autowired
	private EquipRepairRepositoryDataHandler equipRepairRepositoryDataHandler;

	@Override
	public ResultBean save(@RequestBody EquipRepairRepositoryRepairStep obj) {
		return EquipRepairRepositoryRepairStepDataHandler.save(obj);
	}

	@Override
	public ResultBean delete(@RequestBody EquipRepairRepositoryRepairStep obj) {
		return EquipRepairRepositoryRepairStepDataHandler.delete(obj);
	}

	@Override
	public ResultBean update(@RequestBody EquipRepairRepositoryRepairStep obj) {
		return EquipRepairRepositoryRepairStepDataHandler.update(obj);
	}

	@Override
	public ResultBean save(@RequestBody List<EquipRepairRepositoryRepairStep> list) {
		ResultBean resultBean = new ResultBean(true);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("equip_category_id", list.get(0).getEquipCategoryId());
		map.put("fault_cause_id", list.get(0).getFaultCauseId());
		map.put("row_status", 1);
		Collection<EquipRepairRepositoryEntity> listByMap = equipRepairRepositoryDataHandler.listByMap(map);
		if (listByMap != null && listByMap.size() > 0) {
			// 主表信息已存在
			long equipRepairRepositoryId = 0;
			for (EquipRepairRepositoryEntity item : listByMap) {
				equipRepairRepositoryId = item.getId();
			}
			for (EquipRepairRepositoryRepairStep item : list) {
				item.getInnerData().setEquipRepairRepositoryId(equipRepairRepositoryId);
			}

		} else {
			// 主表信息不存在
			EquipRepairRepository equipRepairRepository = new EquipRepairRepository();
			EquipRepairRepositoryEntity equipRepairRepositoryEntity = new EquipRepairRepositoryEntity();
			equipRepairRepositoryEntity.setEquipCategoryId(list.get(0).getEquipCategoryId());
			equipRepairRepositoryEntity.setFaultCauseId(list.get(0).getFaultCauseId());
			equipRepairRepository.setInnerData(equipRepairRepositoryEntity);
			ResultBean resultBean2 = equipRepairRepositoryDataHandler.save(equipRepairRepository);
			if (!resultBean2.success) {
				return resultBean2;
			}
			long equipRepairRepositoryId = Long.valueOf(resultBean2.getMessage());
			for (EquipRepairRepositoryRepairStep item : list) {
				item.getInnerData().setEquipRepairRepositoryId(equipRepairRepositoryId);
			}
		}
		ResultBean save = EquipRepairRepositoryRepairStepDataHandler.save(list);
		if (!save.success) {
			return save;
		}
		return resultBean;
	}

	@Override
	public ResultBean update(@RequestBody List<EquipRepairRepositoryRepairStep> list) {
		return EquipRepairRepositoryRepairStepDataHandler.update(list);
	}

	@Override
	public ResultBean delete(@RequestBody List<EquipRepairRepositoryRepairStep> list) {
		return EquipRepairRepositoryRepairStepDataHandler.delete(list);
	}

	@Override
	public EquipRepairRepositoryRepairStep getById(@PathVariable long id) {
		return EquipRepairRepositoryRepairStepDataHandler.getById(id);
	}

	@Override
	public ResultBean selectByCondition(@RequestBody EquipRepairRepository obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev resultDev = EquipRepairRepositoryRepairStepDataHandler.selectByCondition(obj);
		resultBean.setObject(resultDev);
		return resultBean;
	}
	
	@Override
	public ResultBean moveUp(@RequestBody EquipRepairRepositoryRepairStep obj) {
		// TODO Auto-generated method stub
		return EquipRepairRepositoryRepairStepDataHandler.moveUp(obj);
	}
	
	@Override
	public ResultBean moveDown(@RequestBody EquipRepairRepositoryRepairStep obj) {
		// TODO Auto-generated method stub
		return EquipRepairRepositoryRepairStepDataHandler.moveDown(obj);
	}

}
